package com.pucheng.bracelet.ui.activity.clock

import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.ext.CLOCK_DATA_INDEX_KEY
import com.pucheng.bracelet.app.ext.CLOCK_DATA_KEY
import com.pucheng.bracelet.app.util.click
import com.pucheng.bracelet.databinding.ActivityClockEditBinding
import com.pucheng.bracelet.ui.adapter.ClockData
import com.pucheng.bracelet.viewmodel.clock.ClockViewModel
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity


class ClockEditActivity : BaseVmDbActivity<ClockViewModel, ActivityClockEditBinding>() {
    private lateinit var mClockViewModel: ClockViewModel
    override fun layoutId(): Int = R.layout.activity_clock_edit

    override fun initView(savedInstanceState: Bundle?) {
        val editClockDataIndex = intent.getIntExtra(CLOCK_DATA_INDEX_KEY, 0)
        val editClockData = intent.getSerializableExtra(CLOCK_DATA_KEY) as? ClockData
        mClockViewModel = ViewModelProvider(this).get(ClockViewModel::class.java)
        mDatabind.apply {
            viewModel = mClockViewModel
            lifecycleOwner = this@ClockEditActivity
            tvCancel.click {
                finish()
            }
            tvSave.click {
                mViewModel.saveClock()
                finish()
            }
/*            tpClockEdit.setOnTimeChangedListener { timePicker: TimePicker, hour: Int, minute: Int ->
                LogUtils.e("timePicker:$timePicker | hour:$hour | minute:$minute")
            }
            changeClockStyle(tpClockEdit)*/
            wvAmpm.setCyclic(false)
            wvHour.setCyclic(false)
            wvMinute.setCyclic(false)
            wvAmpm.setOnItemSelectedListener { index ->
                mViewModel.mApIndex.value = index
            }
            wvHour.setOnItemSelectedListener { index ->
                mViewModel.mHourIndex.value = index
            }
            wvMinute.setOnItemSelectedListener { index ->
                mViewModel.mMinuteIndex.value = index
            }

            mViewModel.apply {
                initTime(editClockDataIndex, editClockData)
                getWeekRepeatData(editClockData!!.weekRepeat)
                timeAp.observe(this@ClockEditActivity) {
                    wvAmpm.adapter = ArrayWheelAdapter(it)
                }
                timeHour.observe(this@ClockEditActivity) {
                    wvHour.adapter = ArrayWheelAdapter(it)
                }
                timeMinute.observe(this@ClockEditActivity) {
                    wvMinute.adapter = ArrayWheelAdapter(it)
                }
                timeClock.observe(this@ClockEditActivity) {
                    wvAmpm.currentItem = it[0]
                    wvHour.currentItem = it[1]
                    wvMinute.currentItem = it[2]
                }
            }
        }

    }


    override fun createObserver() {
    }

    override fun dismissLoading() {
    }

    override fun showLoading(message: String) {

    }

/*    private fun changeClockStyle(timePicker: TimePicker) {
        val systemResources: Resources = Resources.getSystem()
        val amPmNumberPickerId: Int = systemResources.getIdentifier("amPm", "id", "android")
        val hourNumberPickerId: Int = systemResources.getIdentifier("hour", "id", "android")
        val minuteNumberPickerId: Int = systemResources.getIdentifier("minute", "id", "android")
        val dividerId: Int = systemResources.getIdentifier("divider", "id", "android")

        val amPmNumberPicker =
            timePicker.findViewById(amPmNumberPickerId) as NumberPicker
        val hourNumberPicker =
            timePicker.findViewById(hourNumberPickerId) as NumberPicker
        val minuteNumberPicker =
            timePicker.findViewById(minuteNumberPickerId) as NumberPicker
        val divider =
            timePicker.findViewById(dividerId) as TextView
        divider.visibility = View.INVISIBLE
        setNumberPickerDivider(amPmNumberPicker)
        setNumberPickerDivider(hourNumberPicker)
        setNumberPickerDivider(minuteNumberPicker)

    }*/

    /*   private fun setNumberPickerDivider(numberPicker: NumberPicker) {
           val pickerFields =
               NumberPicker::class.java.declaredFields
           for (pf in pickerFields) {
               if (pf.name == "mSelectionDivider") {  //设置颜色
                   pf.isAccessible = true
                   val colorDrawable = ColorDrawable(
                       ContextCompat.getColor(this, R.color.colorGray)
                   )
                   try {
                       pf[numberPicker] = colorDrawable
                   } catch (e: IllegalAccessException) {
                       e.printStackTrace()
                   }
               }
               if (pf.name == "mSelectionDividerHeight") {   //设置高度
                   pf.isAccessible = true
                   try {
                       val result = 1
                       pf[numberPicker] = result
                   } catch (e: Exception) {
                       e.printStackTrace()
                   }
                   break
               }
   //            if (pf.name == "mInputText") {   //选中字体
   //                pf.isAccessible = true
   //                try {
   //                    val editText = pf[numberPicker] as EditText
   //                    editText.setTextColor(Color.parseColor("#24DAC5"))
   //                    editText.setBackgroundColor(Color.parseColor("#FFFFFF"))
   //
   //                    pf[numberPicker] = editText
   //                } catch (e: Exception) {
   //                    e.printStackTrace()
   //                }
   //                break
   //            }
               numberPicker.invalidate()
           }
       }*/
}